﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using DataAccess.DataObject;
using System.Collections.Generic;

namespace EnmineKaitlin.admin
{
    public partial class order : Common.PageBase
    {
        const string module = "manage-post";
        const int pagesize = 10;
        int currentpage = 1;
        public int totalpage = 0;        
        string Status = "";
        string fromdate = "2012/01/01 12:00";
        string todate = "9999/12/12 12:00";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (CheckPermission(module))
            {
                if (!IsPostBack)
                {
                    lblFunction.Text = "Hóa Đơn";
                    GetParam();                    
                    BindOrder();
                }
            }
            else
            {
                Response.Redirect("~/admin/permission-fail.aspx");
            }
        }
        protected void GetParam()
        {           
            if (Request["fromdate"] != null && Request["fromdate"].ToString() != "")
            {
                fromdate = Request["fromdate"].ToString();
            }
            if (Request["todate"] != null && Request["todate"].ToString() != "")
            {
                todate = Request["todate"].ToString();
            }
            if (Request["status"] != null && Request["status"].ToString() != "")
            {
                Status = Request["status"].ToString();
                ddlStatus.SelectedValue = Status;
                ddlStatus.DataBind();
            }            
            if (Request["currentpage"] != null && Request["currentpage"].ToString() != "")
            {
                currentpage = int.Parse(Request["currentpage"].ToString());
            }
        }        
        protected void BindOrder()
        {
            IList<OOrder> lstOrder = _BOrder.Get(0,Status,DateTime.Parse(fromdate),DateTime.Parse(todate),"DESC", "CreateDate", currentpage, pagesize);
            grvOrder.DataSource = lstOrder;
            grvOrder.DataBind();

            if (lstOrder.Count > 0)
            {
                ctlPagging.PageSize = pagesize;
                ctlPagging.ItemCount = (int)lstOrder[0].TotalResult;
                string url = "";                
                if (Status != "") { url += "&status=" + Status; }
                if (fromdate.ToString() != "") { url += "&fromdate=" + fromdate; }
                if (todate.ToString() != "") { url += "&fromdate=" + todate; }
                ctlPagging.QueryStringParameterName = url;
                ctlPagging.CurrentIndex = currentpage;
            }
        }
        protected void grvOrder_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            AddCheckBoxAll(e);
        }

        protected void grvOrder_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            if (_BOrder.Delete(int.Parse(grvOrder.DataKeys[e.RowIndex].Value.ToString())))
            {
                BindOrder();
            }
        }
        protected void btnFilter_Command(object sender, CommandEventArgs e)
        {
            if (e.CommandName == "Filter")
            {
                if (txtFromCreateDate.Text != "")
                {
                    fromdate = txtFromCreateDate.Text;
                }
                if (txtToCreateDate.Text != "")
                {
                    todate = txtToCreateDate.Text;
                }
                Status = ddlStatus.SelectedValue;                
                BindOrder();
            }
        }
        protected void lbtFunction_Click(object sender, CommandEventArgs e)
        {
            if (e.CommandName == "BunkFunction")
            {
                string FunctionName = "";
                if (e.CommandArgument.ToString() == "TopFunction")
                {
                    FunctionName = ddlFunctionTop.SelectedValue;
                }
                else if (e.CommandArgument.ToString() == "BottomFunction")
                {
                    FunctionName = ddlFunctionBottom.SelectedValue;
                }
                switch (FunctionName)
                {
                    case "delete":
                        foreach (GridViewRow gvr in grvOrder.Rows)
                        {
                            if (((CheckBox)gvr.Cells[0].Controls[0]).Checked)
                            {
                                _BOrder.Delete(int.Parse(grvOrder.DataKeys[gvr.RowIndex].Value.ToString()));
                            }
                        }
                        BindOrder();
                        break;
                }

            }
        }                
    }
}